// SPU管理模块接口

import request from "@/utils/request";
import type {
  SpuData,
  HasSpuResponseData,
  AllTradeMark,
  SpuHasImg,
  SaleAttrResponseData,
  HasSaleAttrResponseData,
  SkuData,
  SkuInfoData,
} from "./type";

enum API {
  HASSPU_URL = "/admin/product/", //获取已有的SPU数据
  ALLTRADEMARK_URL = "/admin/product/baseTrademark/getTrademarkList", //获取全部品牌的数据
  IMAGE_URL = "/admin/product/spuImageList/", //获取某一个SPU下的全部售卖商品图片的数据
  SPUHASSALEATTE_ULR = "/admin/product/spuSaleAttrList/", //获取某一个SPU下的全部已有的销售属性数据
  ALLSALEATTR_URL = "/admin/product/baseSaleAttrList", //获取整个项目全部的销售属性
  ADDSPU_URL = "/admin/product/saveSpuInfo", //追加一个新的SPU
  UPDATESPU_URL = "/admin/product/updateSpuInfo", //更新已有的SPU
  ADDSKU_URL = "/admin/product/saveSkuInfo", //追加一个新的SKU地址
  SKUINFO_URL = "/admin/product/findBySpuId/", //查看某一个已有的SPU下全部售卖的商品
  REMOVESPU_URL = "/admin/product/deleteSpu/", //删除已有的SPU
}

// 获取某一个三级分类下已有的SPU数据
export const reqHasSpu = (
  page: number,
  limit: number,
  category3Id: number | string
) =>
  request.get<any, HasSpuResponseData>(
    API.HASSPU_URL + `${page}/${limit}?category3Id=${category3Id}`
  );

// 获取全部品牌的SPU数据接口
export const reqAllTradeMark = () =>
  request.get<any, AllTradeMark>(API.ALLTRADEMARK_URL);

// 获取某一个SPU下的全部售卖商品图片的数据接口
export const reqSpuImageList = (spuId: number) =>
  request.get<any, SpuHasImg>(API.IMAGE_URL + spuId);

// 获取某一个SPU下的全部已有的销售属性数据接口
export const reqSpuHasSaleAttr = (spuId: number) =>
  request.get<any, SaleAttrResponseData>(API.SPUHASSALEATTE_ULR + spuId);

// 获取整个项目全部的销售属性接口
export const reqAllSaleAttr = () =>
  request.get<any, HasSaleAttrResponseData>(API.ALLSALEATTR_URL);

// 追加一个新的SPU | 更新已有的SPU
export const reqAddOrUpdateSPU = (data: SpuData) => {
  if (data.id) {
    // 如果SPU对象有id跟新已有的SPU
    return request.post<any, any>(API.UPDATESPU_URL, data);
  } else {
    // 新增
    return request.post<any, any>(API.ADDSPU_URL, data);
  }
};

// 添加SKU请求方法
export const reqAddSku = (data: SkuData) =>
  request.post<any, any>(API.ADDSKU_URL, data);

// 获取SKU数据
export const reqSkuList = (supId: number | string) =>
  request.get<any, SkuInfoData>(API.SKUINFO_URL + supId);

// 删除已有的SPU
export const reqRemoveSpu = (spuId: number | string) =>
  request.delete<any, any>(API.REMOVESPU_URL + spuId);
